/**
 * @Description: node服务
 * @Author: zpl
 */
//-----node依赖
const jsdom = require('jsdom');
const {JSDOM} = jsdom;
const {document} = (new JSDOM('<!doctype html><html><body></body></html>')).window;
global.document = document;
const window = document.defaultView;
const $ = require('jquery')(window);
var express = require('express');
var router = express();
// var router = express.Router();
var mysql = require('mysql');
//-----

$(document).ready(function () {
    //数据库配置
    var pool = mysql.createPool({
        host: '127.0.0.1',
        user: 'root',
        password: 'root123',
        port:'3306',
        database: 'testdb'
    });
    var config = {
        init: function () {
            var self = this;
            //数据库配置
            // self.dbConfig();
            self.event();

        },
        event: function () {
            var self = this;
            self.showData();
            //查询
            self.searchData();
            //添加
            self.insertData();
            $("body").append("insert into person(name,age,professional) values('" + 1 + "'," + 1 + ",'" + 1 + "')");
            console.log($("body").html());
        },
        //config首页
        showData: function () {
            var self = this;
            router.get('/', function (req, res, next) {
                self.query('select * from person', function (err, rows) {
                    console.log(rows);
                    if (err) {
                        res.render('persons', {title: '人员管理', datas: []});  // this renders "views/persons.html"
                    } else {

                        res.render('persons', {title: '人员管理', datas: rows});
                    }
                });
            });

        },
        //查询
        searchData:function(){
            var self = this;
            router.post('/search', function (req, res) {
                var name = req.body.s_name;
                var age = req.body.s_age;
                var professional = req.body.s_professional;

                var sql = "select * from person";

                if (name) {
                    sql += " and name like '%" + name + "%' ";
                }
                if (age) {

                    sql += " and age=" + age + " ";
                }
                if (professional) {
                    sql += " and name like '%" + professional + "%' ";
                }
                sql = sql.replace("and","where");
                self.query(sql, function (err, rows) {
                    if (err) {
                        res.end("查询失败：", err)
                    } else {
                        res.render("persons", {title: '人员管理', datas: rows, s_name: name, s_age: age});
                    }
                });
            });
        },
        //新增
        insertData: function(){
            var self = this;
            router.get('/toAdd', function (req, res) {
                res.render('add');
            });
            router.post('/add', function (req, res) {
                var name = req.body.name;
                var age = req.body.age;
                var professional = req.body.professional;
                self.query("insert into person(name,age,professional) values('" + name + "'," + age + ",'" + professional + "')", function (err, rows) {
                   debugger
                    if (err) {
                        res.end('新增失败：' + err);
                    } else {
                        res.redirect('/config');
                    }
                })
            });
        },
        updateData:function(){

        },
        deleteData:function (){

        },
        //数据库操作
        query:function (sql,callback) {
            var self = this;
            pool.getConnection(function (err, connection) {
                // Use the connection
                connection.query(sql, function (err, rows) {
                    callback(err, rows);
                    connection.release();//释放链接
                });
            });
            exports.query =self.query;
        },
    };
debugger
    config.init();

});



module.exports = router;